package com.volvo.caretrack.mobile.services;

import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

import com.volvo.caretrack.mobile.exception.AuthException;
import com.volvo.caretrack.mobile.feed.FeedDeskFactory;
import com.volvo.caretrack.mobile.feed.IMachineManagerDesk;
import com.volvo.caretrack.mobile.model.auth.Session;
import com.volvo.caretrack.mobile.model.machine.Machine;
import com.volvo.caretrack.mobile.model.machine.MachinePosition;
import com.volvo.caretrack.mobile.model.machine.SearchCondition;

public class MachineManagerService {
	
	private static AtomicReference<MachineManagerService> _instance = new AtomicReference<MachineManagerService>();
	
	IMachineManagerDesk machineManagerDesk;
	
	private MachineManagerService() {
		initialize();
	}
	
	private void initialize() {
		machineManagerDesk = FeedDeskFactory.getMachineManagerDesk();
	}
	
	public static MachineManagerService getInstance()
    {
		MachineManagerService m = _instance.get();
        if (null == m)
        {
            _instance.compareAndSet(null, new MachineManagerService());
            m = _instance.get();
        }
        return m;
    }
	
	public List<Machine> searchMachineByDealerID(String sessionID, String dealerId) throws AuthException {
		Session session = new Session(sessionID);
		SearchCondition condition = new SearchCondition().setDealerId(dealerId);
		List<Machine> list = machineManagerDesk.getMachines(session, condition);
		return list;
	}
	
	public MachinePosition getMachinePosition(String sessionID, String chassisNo) throws AuthException {
		Session session = new Session(sessionID);
		MachinePosition position = machineManagerDesk.getPosition(session, chassisNo);
		return position;
	}

}
